/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.web.admin;

import com.jfinal.aop.Inject;
import com.jfinal.kit.JsonKit;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.permission.annotation.PermissionDef;
import io.jpress.service.UtmService;
import io.jpress.web.AdminPermissionGroups;
import io.jpress.web.base.AdminControllerBase;

import java.util.Map;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Title: 网站统计相关的 Controller
 */
@RequestMapping(value = "/admin/statistics", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _WebsiteStatisticsController extends AdminControllerBase {

    @Inject
    private UtmService utmService;


    /**
     * 网站访问量
     */
    @PermissionDef(title = "查看站点访问量统计", groupId = AdminPermissionGroups.STATISTICS)
    @AdminMenu(text = "站点访问量统计", groupId = JPressConsts.SYSTEM_MENU_STATISTICS, order = 9999)
    public void website() {
        Map<String, Long> ipIn7Days = utmService.queryIpIn7Days();
        Map<String, Long> ipMapIn7Days = utmService.queryIpMapIn7Days();
        Map<String, Long> pvIn7Days = utmService.queryPvIn7Days();
        Map<String, Long> uvIn7Days = utmService.queryUvIn7Days();

        setAttr("ipIn7Days", JsonKit.toJson(ipIn7Days));
        setAttr("ipMapIn7Days", JsonKit.toJson(ipMapIn7Days));
        setAttr("pvIn7Days", JsonKit.toJson(pvIn7Days));
        setAttr("uvIn7Days", JsonKit.toJson(uvIn7Days));

        setAttr("todayIp", getLast(ipIn7Days));
        setAttr("yesterdayIp", getSecondLast(ipIn7Days));
        setAttr("totalIp", totalCount(ipIn7Days));

        setAttr("todayPv", getLast(pvIn7Days));
        setAttr("yesterdayPv", getSecondLast(pvIn7Days));
        setAttr("totalPv", totalCount(pvIn7Days));

        setAttr("todayUv", getLast(uvIn7Days));
        setAttr("yesterdayUv", getSecondLast(uvIn7Days));
        setAttr("totalUv", totalCount(uvIn7Days));

        render("statistics/website.html");
    }

    private Long getLast(Map<String, Long> dataMap) {
        Map.Entry<String, Long> lastEntry = (Map.Entry<String, Long>) dataMap.entrySet().toArray()[dataMap.size() - 1];
        return lastEntry.getValue();
    }

    private Long getSecondLast(Map<String, Long> dataMap) {
        Map.Entry<String, Long> secondLastEntry = (Map.Entry<String, Long>) dataMap.entrySet().toArray()[dataMap.size() - 2];
        return secondLastEntry.getValue();
    }

    private Long totalCount(Map<String, Long> dataMap){
        long count = 0;
        for (Long value : dataMap.values()) {
            if (value != null){
                count += value;
            }
        }
        return count;
    }


}
